const Router = require('@koa/router');
const Koa = require('koa'); //导入库文件，moduel export导出  
const koaBody = require('koa-body')
const cors = require("@koa/cors")
const userRouter = require('./routes/user');
const jindianRouter = require('./routes/jindian');
const areaRouter = require('./routes/area');
const {
  sequelize
} = require('./database/index');
const path = require('path')
const upload = require('./routes/upload')
const koaStatic = require('koa-static')

const app = new Koa();
const router = new Router();

//自执行函数
(async () => { //模型同步到数据库(ORM映射)
  await sequelize.sync({
    force: false
  }) //force: true =>每次新建表
})()

// 在app.js中组装子路由
router.use("/user", userRouter.routes());
router.use("/jindian", jindianRouter.routes());
router.use("/area", areaRouter.routes());
router.use(upload.routes())
app.use(koaBody({
  multipart: true,
  formidable: {
    uploadDir: path.join(__dirname, './uploads'), //配置保存路径
    keepExtensions: true //保存文件扩展名
  }
}));
app.use(cors()); //允许跨域中间件

app.use(router.routes());
app.use(router.allowedMethods());
app.use(koaStatic(path.join(__dirname, './uploads')))

app.listen(3000); //koa自带服务器端口3000